﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zhaoxi.FactoryDeviceSystem.Models;
using Zhaoxi.FactoryDeviceSystem.Utils;

namespace Zhaoxi.FactoryDeviceSystem
{
    public partial class FrmPLCSetting : Form
    {
        public FrmPLCSetting()
        {
            InitializeComponent();
        }
        private void FrmPLCSetting_Load(object sender, EventArgs e)
        {
            if(CommonHelper.plcSet==null)
            {
                if(!Directory.Exists(CommonHelper.folder))
                    Directory.CreateDirectory(CommonHelper.folder);
                //没有配置plc，设置默认设置
                cboCpuTypes.SelectedIndex = 0;
                txtIp.Clear();
                txtPort.Text = "102";
                txtRack.Text = "0";
                txtSlot.Text = "0";
            }
            else
            {
                //已有配置了,加载已有配置
                PLCInfo plcSet = CommonHelper.plcSet;
                cboCpuTypes.SelectedItem = plcSet.CpuType.ToString();
                txtIp.Text=plcSet.Ip;
                txtPort.Text = plcSet.Port.ToString();
                txtRack.Text=plcSet.Rack.ToString();
                txtSlot.Text=plcSet.Slot.ToString();
            }
        }

        //保存配置信息
        private void btnOk_Click(object sender, EventArgs e)
        {
            //接收输入信息
            string cpuType = cboCpuTypes.Text;
            string ip = txtIp.Text.Trim();
            string port=txtPort.Text.Trim();
            string rack=txtRack.Text.Trim();
            string slot=txtSlot.Text.Trim();
            string[] ipArr=ip.Split('.');
            if(ipArr.Length !=4) {
                MessageHelper.Error("Plc配置", "IP地址输入格式不正确！");
                return;
            }
            if(int.TryParse(port,out int intPort)==false)
            {
                MessageHelper.Error("Plc配置", "端口号输入格式不正确！");
                return;
            }
            if (short.TryParse(rack, out short intRack) == false)
            {
                MessageHelper.Error("Plc配置", "机架号输入格式不正确！");
                return;
            }
            if (short.TryParse(slot, out short intSlot) == false)
            {
                MessageHelper.Error("Plc配置", "插槽号输入格式不正确！");
                return;
            }
            //拼成字符串
            string setString = $"CpuType:{cpuType};Ip:{ip};port:{port};rack:{rack};slot:{slot}";
            //写入文件
            File.WriteAllText(CommonHelper.communicatePath, setString);
            MessageHelper.Success("Plc配置", "Plc通信设置保存成功！");
            //封装到plcSet对象中
            CommonHelper.plcSet = new PLCInfo()
            {
                CpuType = (CpuType)Enum.Parse(typeof(CpuType), cpuType),
                Ip = ip,
                Port = intPort,
                Rack = intRack,
                Slot = intSlot
            };
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
